<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ToDoView.php 320 2008-11-23 07:18:37Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalToDoRecord');
Prado::using('Application.Common.Data.PortalToDoHistoryRecord');
Prado::using('Application.Common.Data.PortalUserRecord');

class ToDoView extends PortalAdminPage
{
	private $_todorecord	= null;

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('View ToDo');
		if(!$this->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function getToDoRecord()
	{
		if($this->_todorecord === null)
		{
			$this->_todorecord	= PortalToDoRecord::finder()->findByid($this->Request['id']);
		}

		return $this->_todorecord;
	}

	public function dataBind()
	{
		// Load history
		$this->Repeater->DataSource	= PortalToDoHistoryRecord::finder()->findAll("todoid=?", $this->Request['id']);
		$this->Repeater->dataBind();

		$this->ddlAssignedTo->DataSource	= PortalUserRecord::finder()->FindAll('role = ?' , $this->Application->Parameters['AdminRole']);
		$this->ddlAssignedTo->dataBind();
	}

	public function SaveToDoHistory($sender, $param)
	{
		if($this->IsValid)
		{
			$th = new PortalToDoHistoryRecord();
			$th->todoid		= $this->ToDoRecord->id;
			$th->answer		= $this->tbDescription->Text;
			$th->created	= time();
			$th->userid		= $this->User->Id;
			$th->save();

			// Update todo
			$this->ToDoRecord->deadline		= $this->dpDeadline->TimeStamp;
			$this->ToDoRecord->priority		= $this->ddlPriority->SelectedValue;
			$this->ToDoRecord->assignedto	= $this->ddlAssignedTo->SelectedValue;
			$this->ToDoRecord->status		= $this->ddlStatus->SelectedValue;
			$this->ToDoRecord->save();
		}

		$this->gotoPage('Admin.Todo');
	}
}
?>